import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    component: () => import('../pages/login/login.vue'),
    meta: {
      title: '手机登录'
    }
  },
  {
    path: '/register',
    component: () => import('../pages/register/register.vue'),
    meta: {
      title: '注册'
    }
  },
  {
    path: '/index',
    component: () => import('../pages/index/index.vue'),

    children: [
      {
        path: 'home',
        component: () => import('../pages/home/home.vue'),
        meta: {
          title: '小u商城'
        },
      },
      {
        path: 'shopCar',
        component: () => import('../pages/shopCar/shopCar.vue'),
        meta: {
          title: '购物车'
        },
      },
      {
        path: 'mine',
        component: () => import('../pages/mine/mine.vue'),
        meta: {
          title: '我的'
        },
      },
      {
        path: 'cate',
        component: () => import('../pages/cate/cate.vue'),
        meta: {
          title: '分类'
        },
      },
      {
        path: '',
        redirect: 'home'
      }
    ]
  },
  {
    path: '/list',
    component: () => import('../pages/list/list.vue'),
    meta: {
      title: '商品列表'
    },
  },

  {
    path: '/detail',
    component: () => import('../pages/detail/detail.vue'),
    meta: {
      title: '商品详情'
    },
  },
  {
    path: '/order',
    component: () => import('../pages/order/order.vue'),
    meta: {
      title: '确认订单'
    },
  },
  {
    path: '/address',
    component: () => import('../pages/address/address.vue'),
    meta: {
      title: '地址管理'
    },
  },
  {
    path: '/newAddress',
    component: () => import('../pages/newAddress/newAddress.vue'),
    meta: {
      title: '新增地址'
    },
  },
  {
    path: '/pay',
    component: () => import('../pages/pay/pay.vue'),
    meta: {
      title: '支付'
    },
  },
  {
    path: '/search',
    component: () => import('../pages/search/search.vue'),
    meta: {
      title: '搜索'
    },
  },
  {
    path: '*',
    component: () => import('../pages/login/login.vue')
  }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  if (to.path === '/login') {
    next()
    return
  }
  if (localStorage.getItem('islogin')) {
    next()
    return
  }
  next('/login')
})

export default router
